<?php
include_once("../includes/page_utf_class.php");
include_once("../module/product/includes/plugin_order_class.php");
include("../lang/cn/company_type_config.php");
include_once($config['webroot']."/lib/phpexcel/Classes/PHPExcel/IOFactory.php");
ini_set('memory_limit', '128M');
$line = 2;
if (!file_exists($config['webroot']."/lib/phpexcel/order_demo.xls")) {
    exit("Can not find Excel template!");
} else {
    $objPHPExcel = PHPExcel_IOFactory::load($config['webroot']."/lib/phpexcel/order_demo.xls");
}
$objPHPExcel->getSheet(0)->setTitle('订单列表');
$param = $_GET;
$order = new order;

if($param['code'])
{
    $str.=" and  order_id like '$param[code]%' ";
}
if(isset($param['status']) and is_numeric($param['status']))
{
    $status=$param['status']-1;
    $str.=" and  status ='$status' ";
}
if($param['user'])
{
    $str.=" and user like '%$param[user]%' ";
}
if($param['ship_name'])
{
    $str.=" and ship_name like '%$param[ship_name]%' ";
}
if($param['ship_addr'])
{
    $str.=" and ship_addr like '%$param[ship_addr]%' ";
}
if($param['order_ship_status'])
{
    $str.=" and ship_status = '$param[order_ship_status]' ";
}
if($param['order_pay_status'])
{
    $str.=" and pay_status = '$param[order_pay_status]' ";
}
if($param['pay'])
{
    $str.=" and payment_id = '$param[pay]' ";
}
if($param['ship'])
{
    $str.=" and shipping_id  = '$param[ship]' ";
}
if($param['maxp'])
{
    $str.=" and cost+freight <= '$param[maxp]' ";
}
if($param['minp'])
{
    $str.=" and cost+freight >= '$param[minp]' ";
}

$sql="select pay_status,freight,payment_name,shipping_name,ship_mobile,ship_status,order_id,ship_name,cost,a.status,create_time,user from ".ORDER." a left join ".MEMBER." b on a.member_id=b.userid where 1 $str order by id desc";
//=============================
$db->query($sql);
$ss=$db->getRows();
foreach($ss as $k)
{
    $k['sum']=$k['cost']+$k['freight'];
    $k['statu_text']=$order->get_shop_order_statu($k['status']);
    $k['order_pay_status']=$order_pay_status[$k['pay_status']-1];
    $k['order_ship_status']=$order_ship_status[$k['ship_status']-1];
    $list[]=$k;
}

foreach ($list as $v) {

    $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValueExplicit("A" . "$line", $v['order_id'], PHPExcel_Cell_DataType::TYPE_STRING)
        ->setCellValue("B" . "$line", number_format($v['sum'],2))
        ->setCellValueExplicit("C" . "$line", $v['user'], PHPExcel_Cell_DataType::TYPE_STRING)
        ->setCellValueExplicit("D" . "$line", $v['ship_name'], PHPExcel_Cell_DataType::TYPE_STRING)
        ->setCellValueExplicit("E" . "$line", date("Y-m-d H:m:s",$v['create_time']), PHPExcel_Cell_DataType::TYPE_STRING)
        ->setCellValueExplicit("F" . "$line", $v['statu_text'], PHPExcel_Cell_DataType::TYPE_STRING)
        ->setCellValueExplicit("G" . "$line", $v['order_pay_status'], PHPExcel_Cell_DataType::TYPE_STRING)
        ->setCellValueExplicit("H" . "$line", $v['order_ship_status'], PHPExcel_Cell_DataType::TYPE_STRING)
        ->setCellValueExplicit("I" . "$line", $v['shipping_name'], PHPExcel_Cell_DataType::TYPE_STRING)
        ->setCellValueExplicit("J" . "$line",$v['payment_name'], PHPExcel_Cell_DataType::TYPE_STRING)
        ->setCellValueExplicit("K" . "$line",$v['ship_mobile'], PHPExcel_Cell_DataType::TYPE_STRING);
    $line++;
}
//设置导出文件名
$filename = '订单列表' . date('YmdHi');


header("Content-type: text/html; charset=utf-8");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $filename . '.xls');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
